package com.ffapp.helper.fragment.passageway;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.ffapp.helper.R;
import com.ffapp.helper.basis.BasisFragment;
import com.ffapp.helper.bean.ParamListData;
import com.ffapp.helper.util.EditTextUtil;
import com.ffapp.helper.util.PassWayUtil;
import com.ffapp.helper.util.SoftKeyBoardListener;
import com.suke.widget.SwitchButton;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link RSADCPropertiesFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RSADCPropertiesFragment extends BasisFragment {

    private String hardwareInterface;
    private List<ParamListData> passWayList;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    MyAdapter adapter;
    ArrayList<ParamListData> showList = new ArrayList<>();


    public static RSADCPropertiesFragment newInstance(String hardwareInterface, List<ParamListData> passWayList) {
        RSADCPropertiesFragment f = new RSADCPropertiesFragment();
        f.hardwareInterface = hardwareInterface;
        f.passWayList = passWayList;
        return f;
    }

    public static RSADCPropertiesFragment newInstance() {
        RSADCPropertiesFragment f = new RSADCPropertiesFragment();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.fragment_rsadcproperties);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        readData(hardwareInterface, passWayList);
    }

    //回读
    public void readData(String hardwareInterface, List<ParamListData> passWayList) {
        if (!TextUtils.isEmpty(hardwareInterface) && passWayList.size() > 1 && isAdded()) {
            showList.clear();
            PassWayUtil.hardwareInterfaceIsChange(hardwareInterface, passWayList);
            switch (hardwareInterface) {
                case "RainFall":
                    for (ParamListData data : passWayList){
                        switch (data.getName()){
                            case "数据采样开关":
                            case "通道标识":
                            case "采集数据单位":
                            case "采集数据精度":
                            case "上电延迟":
                            case "数据存储间隔":
                            case "数据上报间隔":
                                showList.add(data);
                                break;
                        }
                    }
                    break;
                default:
                    for (ParamListData data : passWayList){
                        switch (data.getName()){
                            case "数据采样开关":
                            case "通道标识":
                            case "采样基值":
                            case "采集数据单位":
                            case "采集数据精度":
                            case "上电延迟":
                            case "数据存储间隔":
                            case "数据采样间隔":
                            case "唤醒间隔":
                            case "异常值阀值":
                                showList.add(data);
                                break;
                        }
                    }
                    break;
            }
            adapter.notifyDataSetChanged();
            recyclerView.setAdapter(adapter);
        }

    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        public MyAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_pass_way_base_item, viewGroup, false);
            return new MyAdapter.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int position) {
            viewHolder.list_item.setTag(position);
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return showList == null ? 0 : showList.size();
        }


        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.list_item)
            LinearLayout list_item;
            @BindView(R.id.tv_name)
            TextView tvName;
            @BindView(R.id.edit_text)
            EditText editText;
            @BindView(R.id.img_spanner)
            ImageView imgSpanner;
            @BindView(R.id.tv_unit)
            TextView tvUnit;
            @BindView(R.id.tv_synchro)
            TextView tvSynchro;
            @BindView(R.id.switch_btn)
            SwitchButton switchBtn;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                ParamListData terminalListData = showList.get(position);
                tvName.setText(terminalListData.getName());
                switch (terminalListData.getType()) {
                    case 1: //spinner
                        editText.setText("" + (TextUtils.isEmpty(terminalListData.getValue()) ? terminalListData.getKey() : terminalListData.getValue()));
                        editText.setHint("请选择" + terminalListData.getName());
                        editText.setInputType(terminalListData.getinputType_REAL());
                        imgSpanner.setVisibility(View.VISIBLE);
                        if (!TextUtils.isEmpty(terminalListData.getUnit())) {
                            tvUnit.setVisibility(View.VISIBLE);
                            tvUnit.setText(terminalListData.getUnit());
                        } else {
                            tvUnit.setVisibility(View.GONE);
                        }
                        switchBtn.setVisibility(View.GONE);
                        tvSynchro.setVisibility(View.GONE);
                        editText.setEnabled(true);//不可编辑
                        list_item.setBackgroundResource(R.color.white);
                        break;
                    case 2:  // edit
                        editText.setText("" + terminalListData.getValue());
                        editText.setHint("请输入" + terminalListData.getName());
                        editText.setInputType(terminalListData.getinputType_REAL());
                        tvUnit.setVisibility(View.VISIBLE);
                        tvUnit.setText(terminalListData.getUnit());
                        imgSpanner.setVisibility(View.GONE);
                        switchBtn.setVisibility(View.GONE);
                        tvSynchro.setVisibility(View.GONE);
                        editText.setEnabled(true);//可编辑
                        editText.setVisibility(View.VISIBLE);
                        list_item.setBackgroundResource(R.color.white);
                        break;
                    case 3:  // 开关按钮
                        switch (terminalListData.getValue()) {
                            case "0":
                                switchBtn.setChecked(false);
                                break;
                            case "1":
                                switchBtn.setChecked(true);
                                break;
                        }
                        imgSpanner.setVisibility(View.GONE);
                        tvUnit.setVisibility(View.GONE);
                        tvSynchro.setVisibility(View.GONE);
                        editText.setVisibility(View.INVISIBLE);
                        list_item.setBackgroundResource(R.color.white);
                        switchBtn.setVisibility(View.VISIBLE);
                        switchBtn.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                                if (isChecked == true) {
                                    editText.setText("1");
                                } else {
                                    editText.setText("0");
                                }
                            }
                        });
                        break;
                }
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        int curPosition = getAdapterPosition();
                        ParamListData data = showList.get(curPosition);
                        data.setChange(true);
                        data.setValue(s.toString());
                    }
                });

                imgSpanner.setTag(terminalListData);
                tvSynchro.setTag(terminalListData);
            }

            @OnClick(R.id.img_spanner)
            public void showSpanner(View v) {
                SoftKeyBoardListener.hideKeyboard(getActivity());
                final ParamListData terminalListData = (ParamListData) v.getTag();
                OptionsPickerView pickerView = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        editText.setText(terminalListData.getValues().get(options1));
                    }
                })
                        .setSubmitText("确定")//确定按钮文字
                        .setCancelText("取消")//取消按钮文字
                        .build();
                pickerView.setPicker(terminalListData.getValues());
                pickerView.show();
            }
        }
    }

}
